<?php
namespace app\index\controller;

use service\DataService;
use think\Db;
use think\Controller;
/**
 * @desc 公司信息
 * Class Valet
 * @package app\index\controller
 */
class Company extends Controller
{
    public $table = 'Company';

    public function index(){
        $area = input('get.area');
        if($area){
            cookie('area',$area);
        }else{
            $area = cookie('area');
            $area  = $area ? $area : 2277;
        }
        $this->assign('area', $area);
        $limit = 4;
        $res = Db::table('company')
            ->field('company.*')
            ->Join('area_relation ar','ar.content_id = company.id','left')
            ->where('company.status', 1)
            ->where('ar.type', 2)
            ->where('ar.province_id', $area)
            ->order('company.id desc')
            ->paginate($limit,false,['path' => '/company[PAGE].html']);
        $this->assign('list', $res);

        $province = getAreas();
        $this->assign('province', $province);
        return $this->fetch();
    }

    public function registerCompany(){

        return view();
    }

    public function registerDo(){
        $datas = $this->request->post();

        $code = $datas['code'];
        unset($datas['code']);
        if($code != 123456){
            $data = [
                'code' => 0,
                'msg'   => '验证码错误了哦',
            ];
            return json($data);
        }
        $area = [
            ['province_id'=>1,'city_id'=>2,'county_id'=>[6,7,8,9]],
            ['province_id'=>11,'city_id'=>12,'county_id'=>[16,17,18,19]],
        ];

        $areas = [];

        $datas['create_time'] = date('Y-m-d H:i:s');

        $company_id = DataService::save(Db::name($this->table), $datas, 'id', []);

        if($company_id){
            foreach ($area as $k => $v){
                foreach ($v['county_id'] as $kk => $vv){
                    $areas[] = [
                        'province_id' => $v['province_id'],
                        'city_id'     => $v['city_id'],
                        'county_id'   => $vv,
                        'content_id'  => $company_id,
                        'type'        => 2,
                        'create_time' => date('Y-m-d H:i:s')
                    ];
                }
            }

            Db::table('area_relation')->insertAll($areas);

            $data = [
                'code' => 200,
                'msg'   => '注册成功了！',
            ];
        }else{
            $data = [
                'code' => 0,
                'msg'   => '注册失败',
            ];
        }

        return json($data);

    }

    public function uploads(){
        return uploads();
    }

    public function companyDetail($id){

        $parent_id = -1;
        $res = getAreas($parent_id);
        print_r($res);

        dd();
        return $this->fetch();
    }

    public function agreement(){
        return $this->fetch();
    }

}
